import { Component, OnInit, Input } from '@angular/core';

import { AppGlobalService } from './../../shared/service/app-global.service';
import { ActivatedRoute, Router } from '@angular/router';
import { HttpService } from './../../shared/service/http.service';

@Component({
  selector: 'app-service-agreement-checked',
  templateUrl: './service-agreement-checked.component.html',
  styleUrls: ['./service-agreement-checked.component.scss'],
})
export class ServiceAgreementCheckedComponent implements OnInit {

  @Input() tagData: any;
  constructor(
    public router: Router,
    private route: ActivatedRoute,
    private appGlobalService: AppGlobalService,
    private http: HttpService,
  ) { }

  ngOnInit() {

  }
  agreenmetLink(item) {
    // console.log(item);
    if (item.link.type === "url") {
      this.appGlobalService.openUrl(item.label, item.link.iconUrl);
    } else if (item.link.type === "page") {

      let params = item.link.params ? item.link.params : {};
      if (this.tagData.data.paramName) {
        const keyString: string = this.tagData.data.paramName;
        const keys = keyString.split(',');
        keys.forEach(element => {
          if (localStorage.getItem(element)) {
            const p = JSON.parse(localStorage.getItem(element));
            Object.assign(params, p);
          }
        });
        // tslint:disable-next-line:prefer-const
        let argument = {};
        if (item.link.params.data) {
          item.link.params.data.forEach(element => {
            argument[element.formKey] = element.value;
          });
        }
        params = Object.assign(
          params,
          argument
        );
      }
      /*  console.log(params) */
      const pageParams = {
        pageId: item.link.target,
        params,
        sessionId: localStorage.getItem('sessionId')
      };
      /* console.log(JSON.stringify(pageParams)) */
      this.appGlobalService.goToForward(pageParams);
    } else if (item.link.type === "contract" || item.link.type === "ljcontract") {
      let params = item.link.params ? item.link.params : {};
      if (this.tagData.data.paramName) {
        const keyString: string = this.tagData.data.paramName;
        const keys = keyString.split(',');
        keys.forEach(element => {
          if (localStorage.getItem(element)) {
            const p = JSON.parse(localStorage.getItem(element));
            Object.assign(params, p);
          }
        });
        // tslint:disable-next-line:prefer-const
        let argument = {};
        if (item.link.params.data) {
          item.link.params.data.forEach(element => {
            argument[element.formKey] = element.value;
          });
        }
        params = Object.assign(
          params,
          argument
        );
      }
      /*  console.log(params) */
      const pageParams = {
        pageId: item.link.target,
        params,
        sessionId: localStorage.getItem('sessionId')
      };
      const url = (item.link.type === "ljcontract" ? "element/ljserviceContract/url" : "element/serviceContract/url");
      this.http.post(url, pageParams.params, res => {
        /*  this.appGlobalService.openUrl("", res.url);  */
        this.appGlobalService.openUrl(item.label, res.url, item.link.target, JSON.stringify(params));
      });
    } else if (item.link.type === "decoration") {
      // 调用装修的服务协议
      // tslint:disable-next-line:prefer-const
      let params = item.link.params ? item.link.params : {};
      if (this.tagData.data.paramName) {
        const keyString: string = this.tagData.data.paramName;
        const keys = keyString.split(',');
        keys.forEach(element => {
          if (localStorage.getItem(element)) {
            const p = JSON.parse(localStorage.getItem(element));
            Object.assign(params, p);
          }
        });
        // tslint:disable-next-line:prefer-const
        let argument = {};
        if (item.link.params.data) {
          item.link.params.data.forEach(element => {
            argument[element.formKey] = element.value;
          });
        }
      }
      /*  console.log(params) */
      const pageParams = {
        pageId: item.link.target,
        params,
        sessionId: localStorage.getItem('sessionId')
      };
      // tslint:disable-next-line:prefer-const
      let type = { type: item.link.type };
      pageParams.params = Object.assign(
        pageParams.params,
        type
      );
      this.http.post('element/serviceContract/decorationurl', pageParams.params, res => {
        /*  this.appGlobalService.openUrl("", res.url);  */
        this.appGlobalService.openUrl(item.label, res.url, item.link.target, JSON.stringify(params));
      });
    } else if (item.link.type === "projectInv") {
      // console.log('走？');
      // 调用物业招商的服务协议
      // tslint:disable-next-line:prefer-const
      let params = item.link.params ? item.link.params : {};
      if (this.tagData.data.paramName) {
        const keyString: string = this.tagData.data.paramName;
        const keys = keyString.split(',');
        keys.forEach(element => {
          if (localStorage.getItem(element)) {
            const p = JSON.parse(localStorage.getItem(element));
            Object.assign(params, p);
          }
        });
        // tslint:disable-next-line:prefer-const
        let argument = {};
        if (item.link.params.data) {
          item.link.params.data.forEach(element => {
            argument[element.formKey] = element.value;
          });
        }
      }
      // console.log(params)
      const pageParams = {
        pageId: item.link.target,
        params,
        sessionId: localStorage.getItem('sessionId')
      };
      // tslint:disable-next-line:prefer-const
      let type = { type: item.link.type };
      pageParams.params = Object.assign(
        pageParams.params,
        type
      );
      this.http.post('element/serviceContract/decorationurl', pageParams.params, res => {
        /*  this.appGlobalService.openUrl("", res.url);  */
        this.appGlobalService.openUrl(item.label, res.url, item.link.target, JSON.stringify(params));
      });
    } else if (item.link.type === "propertyInv") {
      // 调用项目招商的服务协议
      // tslint:disable-next-line:prefer-const
      let params = item.link.params ? item.link.params : {};
      if (this.tagData.data.paramName) {
        const keyString: string = this.tagData.data.paramName;
        const keys = keyString.split(',');
        keys.forEach(element => {
          if (localStorage.getItem(element)) {
            const p = JSON.parse(localStorage.getItem(element));
            Object.assign(params, p);
          }
        });
        // tslint:disable-next-line:prefer-const
        let argument = {};
        if (item.link.params.data) {
          item.link.params.data.forEach(element => {
            argument[element.formKey] = element.value;
          });
        }
      }
      /*  console.log(params) */
      const pageParams = {
        pageId: item.link.target,
        params,
        sessionId: localStorage.getItem('sessionId')
      };
      // tslint:disable-next-line:prefer-const
      let type = { type: item.link.type };
      pageParams.params = Object.assign(
        pageParams.params,
        type
      );
      this.http.post('element/serviceContract/decorationurl', pageParams.params, res => {
        /*  this.appGlobalService.openUrl("", res.url);  */
        this.appGlobalService.openUrl(item.label, res.url, item.link.target, JSON.stringify(params));
      });
    }
  }
}
// paramName: "quote_id,quote_name"

